#!/usr/bin/env python3
# -*- coding: utf-8 -*-

def validate_uci_file_with_carriers(file_path):
    """验证包含运营商信息的UCI配置文件"""
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
        
        lines = content.splitlines()
        config_count = content.count("config ")
        option_count = content.count("option ")
        carrier_count = content.count("option 'carrier'")
        plmn_count = content.count("option 'plmn'")
        
        print(f"文件: {file_path}")
        print(f"总行数: {len(lines)}")
        print(f"总字符数: {len(content)}")
        print(f"config块数量: {config_count}")
        print(f"option行数量: {option_count}")
        print(f"carrier字段数量: {carrier_count}")
        print(f"plmn字段数量: {plmn_count}")
        
        # 统计各运营商的配置数量
        carriers = {}
        plmns = {}
        
        for line in lines:
            if "option 'carrier'" in line:
                carrier = line.split("'")[3]
                carriers[carrier] = carriers.get(carrier, 0) + 1
            elif "option 'plmn'" in line:
                plmn = line.split("'")[3]
                plmns[plmn] = plmns.get(plmn, 0) + 1
        
        print("\n运营商分布:")
        for carrier, count in sorted(carriers.items()):
            print(f"  {carrier}: {count} 个配置")
        
        print(f"\nPLMN分布:")
        for plmn, count in sorted(plmns.items()):
            print(f"  {plmn}: {count} 个配置")
        
        # 显示每个运营商的示例配置
        print(f"\n各运营商示例配置:")
        current_config = {}
        in_config = False
        
        for line in lines:
            if line.startswith("config "):
                if current_config and current_config.get('carrier'):
                    # 显示之前的配置示例
                    carrier = current_config['carrier']
                    if not hasattr(validate_uci_file_with_carriers, 'shown_carriers'):
                        validate_uci_file_with_carriers.shown_carriers = set()
                    
                    if carrier not in validate_uci_file_with_carriers.shown_carriers:
                        print(f"\n{carrier} 示例:")
                        print(f"  配置名: {current_config.get('name', 'N/A')}")
                        print(f"  APN: {current_config.get('apn', 'N/A')}")
                        print(f"  PLMN: {current_config.get('plmn', 'N/A')}")
                        print(f"  认证: {current_config.get('auth', 'N/A')}")
                        validate_uci_file_with_carriers.shown_carriers.add(carrier)
                
                current_config = {}
                in_config = True
            elif line.strip().startswith("option ") and in_config:
                parts = line.strip().split("'")
                if len(parts) >= 4:
                    key = parts[1]
                    value = parts[3]
                    current_config[key] = value
            elif line.strip() == "":
                in_config = False
        
        print(f"\n✅ UCI配置文件验证完成！")
        print(f"成功转换了 {config_count} 个APN配置，覆盖 {len(carriers)} 个运营商")
        
    except Exception as e:
        print(f"验证失败: {e}")

if __name__ == "__main__":
    validate_uci_file_with_carriers("apn_config.uci")